package com.ztercelstudio.demo001

import android.util.Log

// variable
fun variable() {
    val data0: Boolean = false

    val data1 = true    // val 定义常量, 类型自行推导
    //data1 = false     // error - val 常量值不可再赋值

    var data2: Int = 1    // var 定义变量
    data2++;       // error - val 常量值不可再赋值

    Log.d("Demo001", "variable declare - val: ${data0}  var: $data2");
}

//Data Type - basic type
fun dataType() {
    // 数值类
    var data0 : Byte = 0
    var data1 : Int = 1 + 1         // UInt 无符号
    var data2 : Short = 10000       // UShort 无符号
    var data3 : Long = 10000000L    // ULong 无符号
    var data4 : Float = 1.23F
    var data5 : Double = 3.1415926

    // 逻辑类
    var data6 : Boolean = true    // Boolean val 定义常量

    // 字符类
    var data7 : Char = 'D'
    var data8 : String = "test"

    // 数组类型
    var data9 : Array<Int> = arrayOf(1, 2, 3)
    Log.d("Demo001", "Byte: $data0 Int $data1 Short $data2 Long $data3 Float $data4 Double ${String.format("%.2f", data5)} Boolean: ${data6} Char: ${data7} String: ${data8} array: ${data9[0]}")

    Log.d("Demo001", "Char ${data0} to Hex: ${String.format("%02X", data7.code)}")
}

//Sentence
fun ifSentence() {

    var data0 = 10
    var data1 = 9

    var result0 = ""
    if (data0 > data1) {
        result0 = "${data0} 更大"
    } else {
        result0 = "${data1} 更大"
    }
    var result1 = if (data0 > data1) "${data0} 更大" else "${data1} 更大"

    Log.d("Demo001", "result0 ${result0}   result1 ${result1}")
}

fun whenSentence() {
    // when中case值必须是常量

    var scope = 100
    var result1 = when(scope) {  // 数值
        0 -> "垃圾"       // 定值
        in 1..60 -> "不及格"       // 范围匹配
        in 61..80 -> "一般"
        in 81..90 -> "良好"
        in 91 .. 99 -> "优秀"
        100 -> "最棒的"
        else -> "分数错误"
    }

    var result2 = when(scope) {
        10,20,30,40,50,60,70,80,90,100 -> "10倍数"    // 序列值匹配
        else -> "非10倍数"
    }

    var result3 = when("萝卜") {  // 字串
        "萝卜" -> "径类"
        "白菜" -> "蔬菜"
        else -> "不晓得"
    }

    var result4 = when(scope) { // is用法
        is Byte -> "Byte"
        is Short -> "Short"
        is Int -> "Int"
        is Long -> "Long"
        else -> "other"
    }

    Log.d("Demo001", "result1: ${result1}   result2: ${result2}  result3: ${result3}  result4: $result4")
}

fun loopSentence() {
    var data = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
    var result1 = ""
    for (i in data) {
        result1 += String.format("%d ", i);
    }
    var result2 = 0
    var i = 0
    do {
        result2 += data[i++];
    } while (i < data.size)

    i = 0
    var result3 = 1
    while (i < data.size) {
        result3 *= data[i++];
    }

    i = 0
    var result4 = ""
    for (i in 0 until data.size step 2) {
        result4 += data[i].toString() + " ";
    }
    i = 0
    var result5 = ""
    for (i in data.size - 1 downTo 0 step 3) {
        result5 += data[i].toString() + " ";
    }

    Log.d("Demo001", "result1: $result1 result2: $result2  result3: $result3 result4: $result4 result5: $result5");
}

fun sentence() {
    Log.d("Demo001", "~~~~~~~ if sentence ~~~~~~~");
    ifSentence();

    Log.d("Demo001", "~~~~~~~ when sentence ~~~~~~~");
    whenSentence()

    Log.d("Demo001", "~~~~~~~ loop sentence ~~~~~~~");
    loopSentence()
}

fun add1(a: Int, b: Int): Int {
    return a + b
}

fun add2(a: Int, b: Int) = a + b

fun add3(a: Int, b: Int = 100) = a + b

fun add4(vararg a: Int): Int {
    Log.d("Demo001", "add4 ${a.size}")
    var sum = 0

    for (i in a) {
       Log.d("Demo001", "value $i")
    }

    for (i in 0 until a.size) {
        sum += a[i]
    }

    return sum;
}

fun operation(a: Int, b: Int, sum: (Int, Int) -> Int, mult: (Int)-> Int): Int {
    return mult(sum(a, b))
}

var add6: (Int, Int) -> Int = {a, b -> a + b}

fun function() {
    Log.d("Demo001", "add1 = ${add1(1, 100)}");
    Log.d("Demo001", "add2 = ${add2(10, 100)}");

    Log.d("Demo001", "add3 = ${add3(1, 99)}");
    Log.d("Demo001", "add3 = ${add3(a=1, b = 99)}");

    Log.d("Demo001", "add4 = ${add4(3, 2, 3, 12)}");

    var sum: (Int, Int)->Int = {a, b -> a + b};
    var mul: (Int)->Int = {a -> a * 10};
    Log.d("Demo001", "operation1 = ${operation(2, 6, sum, mul)}");

    var result = operation(2, 6, {a, b -> a + b}, {a -> a * 10});
    Log.d("Demo001", "operation2 = $result");

    Log.d("Demo001", "add6 = ${add6(2, 6)}");
}

fun sampleEntry() {
    Log.d("Demo001", "----------variable----------");
    variable()

    Log.d("Demo001", "----------dataType----------");
    dataType()

    Log.d("Demo001", "----------sentence----------");
    sentence()

    Log.d("Demo001", "----------function----------");
    function()
}


